//
//  BgView.m
//  Mtime
//
//  Created by Mac on 15/7/31.
//  Copyright (c) 2015年 wxk. All rights reserved.
//

#import "BgView.h"
#import "PosterVCollectionView.h"
#import "IndexCollectionView.h"
#import "HomeModel.h"

@implementation BgView
{

    UIImageView *_headerView;

    UIView *_maskView;// 遮罩视图
    
    PosterVCollectionView *_posterCV;//海报视图
    
    IndexCollectionView *_indexCV;//索引视图
    
    UILabel *titleLabel;//底部电影标题
    
}
- (id)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor redColor];
        
        [self loadSubViews];
        
    }

    return self;
}

- (void)setDataList:(NSMutableArray *)dataList {

    _dataList = dataList;

    //将数据传递给_posterCV
    _posterCV.dataList = _dataList;
    
    _indexCV.dataList = _dataList;

    
    HomeModel *model = _dataList[0];
    
    titleLabel.text = model.titleCn;
    
}

- (void)loadSubViews {
    
    [self loadCollectionView];//加载poster collection view
    
    [self loadLight];//加载镁光灯

    [self loadTitleLabel];//加载电影标题
    
    [self loadMaskView];//加载遮罩视图
    
    [self loadHeaderView];//头部视图
    
    [self addGesture];

    [self addObserver];//给collection view注册观察者

}

- (void)addObserver {

    [_posterCV addObserver:self forKeyPath:@"currentIndexPath" options:NSKeyValueObservingOptionNew context:NULL];
    
    [_indexCV addObserver:self forKeyPath:@"currentIndexPath" options:NSKeyValueObservingOptionNew context:NULL];

}

- (void)loadCollectionView {

    //初始化PosterCollectionView
    _posterCV = [[PosterVCollectionView alloc] initWithFrame:CGRectMake(0, 30, kScreenW, self.frame.size.height -30 -44 -49 -64)];
    
    [self addSubview:_posterCV];
    

}


#pragma mark 添加遮罩视图
- (void)loadMaskView {

    /*
     *下拉头部视图后使界面变暗
     */
    _maskView = [[UIView alloc] initWithFrame:self.bounds];
    
    _maskView.backgroundColor = [UIColor blackColor];
    
    _maskView.hidden = YES;
    
    _maskView.alpha = .5;
    
    [self addSubview:_maskView];

}

#pragma mark 添加镁光灯
- (void)loadLight {

    UIImageView *light1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 124, 204)];
    light1.image = [UIImage imageNamed:@"light"];
    
    [self addSubview:light1];
    
    
    UIImageView *light2 = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenW - 124 -10, 10, 124, 204)];
    light2.image = [UIImage imageNamed:@"light"];
    
    [self addSubview:light2];
    
}

#pragma mark 添加标题视图
- (void)loadTitleLabel {

    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height -44 -49 -64, kScreenW, 44)];

    titleLabel.backgroundColor = [UIColor blackColor];
    
    titleLabel.text = @"title";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:24];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [self addSubview:titleLabel];
}

#pragma mark 添加头部视图
- (void)loadHeaderView {

    _headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -120, kScreenW, 150)];

    _headerView.userInteractionEnabled = YES;//使imageview响应手势
    //stretchableImageWithLeftCapWidth  topCapHeight  图片拉伸
    _headerView.image = [[UIImage imageNamed:@"indexBG_home"] stretchableImageWithLeftCapWidth:0 topCapHeight:5];
    [self addSubview:_headerView];
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(kScreenW/2-100/2, 120, 100, 30);
    
    btn.tag = 1003;
    [btn setImage:[UIImage imageNamed:@"down_home"] forState:UIControlStateNormal];
    
//    [btn setImage:[UIImage imageNamed:@"up_home"] forState:UIControlStateSelected];
    
    [btn addTarget:self action:@selector(downAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [btn setSelected:NO];
    
    [_headerView addSubview:btn];
    
    //添加索引视图
    _indexCV = [[IndexCollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 120)];
    
    [_headerView addSubview:_indexCV];
    
}

- (void)downAction:(UIButton *)sender {
    
    if (sender.selected == NO) {
        
        _maskView.hidden = NO;
        
        [UIView animateWithDuration:.35 animations:^{
            
            _headerView.transform = CGAffineTransformMakeTranslation(0, 120);
            
            sender.transform = CGAffineTransformMakeRotation(M_PI);
            
        }];

    }else {
    
        _maskView.hidden = YES;
        
        [UIView animateWithDuration:.35 animations:^{
            
            _headerView.transform = CGAffineTransformIdentity;
            sender.transform = CGAffineTransformIdentity;
        }];
    }
    sender.selected = !sender.selected;
    
}

#pragma mark 添加滑动手势
- (void)addGesture {
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    
    //上拉手势
    swipe.direction = UISwipeGestureRecognizerDirectionUp;
    
    [self addGestureRecognizer:swipe];
    
    UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    //下拉手势
    swipe2.direction = UISwipeGestureRecognizerDirectionDown;
    
    [self addGestureRecognizer:swipe2];

}

//上下滑动触发方法
- (void)swipe:(UISwipeGestureRecognizer*)ges {
    
    UIButton *btn = (UIButton *)[_headerView viewWithTag:1003];
    
    switch (ges.direction) {//判断滑动手势的滑动方向
        case UISwipeGestureRecognizerDirectionUp://上滑调用隐藏头部视图的方法
        {
            btn.selected = YES;
            [self downAction:btn];
        }
            break;
        case UISwipeGestureRecognizerDirectionDown://下滑调用显示头部视图的方法
        {
            btn.selected = NO;
            [self downAction:btn];
        }
            break;
        default:
            break;
    }
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath//观察的属性名
                      ofObject:(id)object//被观察者
                        change:(NSDictionary *)change//被改变的属性值
                       context:(void *)context {

    
    if ([keyPath isEqualToString:@"currentIndexPath"]) {
        
        //获得监听到的心得属性值
        NSIndexPath *newIndexPath = [change objectForKey:@"new"];
        
        /*
         *如果滚动的是index cv －> poster cv 联动
         *如果滚动的是posetr cv －> index cv 联动
         */
        if ([object isKindOfClass:[IndexCollectionView class]] && _posterCV.currentIndexPath != newIndexPath) {

            [_posterCV scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];

            _posterCV.currentIndexPath = newIndexPath;
            
        }else if ([object isKindOfClass:[PosterVCollectionView class]] && _indexCV.currentIndexPath != newIndexPath) {
        
            [_indexCV scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
            
            _indexCV.currentIndexPath = newIndexPath;
        
        }

        HomeModel *model = _dataList[newIndexPath.item];
        
        titleLabel.text = model.titleCn;
        
    }
}


@end
